package com.wxq.draw;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;

import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;

import android.graphics.Typeface;
import android.graphics.Paint.Style;
import android.text.TextPaint;
import android.text.TextUtils;


import com.wxq.shape.MapPath;
import com.wxq.shape.MapPoint;
import com.wxq.shape.MapShape;

import com.wxq.shape.MapText;

public class DrawViewDataHelper {

	String mapId;
	Context context;
	
	Cursor cursor;
	DrawDBTool dbHelper;
	
	Typeface palmapPublicTF ;
	//绘图元素列表
	public List< Map<String, Object> > shapeList;
	public Map<String, Object> renderMap;
	public Map<String, Object> defaultRender;
	{
		defaultRender = new HashMap<String, Object>();
		defaultRender.put(DrawView.RENDER_STYLE_KEY, 2+"");
	}
	
	public DrawViewDataHelper(Context context, String mapId) {
		super();
		this.context = context;
		this.mapId = mapId;
		this.dbHelper = new DrawDBTool(context);
		palmapPublicTF = Typeface.createFromAsset(context.getAssets(), "PALMAPPUBLIC_0.TTF");
		loadData();
	}

	public DrawViewDataHelper(Context context, String mapId,String filename) {
		super();
		this.context = context;
		this.mapId = mapId;
		this.dbHelper = new DrawDBTool(context);
		this.dbHelper.setDBpath(filename);
		palmapPublicTF = Typeface.createFromAsset(context.getAssets(), "PALMAPPUBLIC_0.TTF");
		loadData();
	}

	
	public void loadData() {
		setPaintList();
		setShapeListWithId(mapId);
		//setShapeList();
	}
	
	
	
	public List<Map<String, Object>> getShapeList() {
		return shapeList;
	}

	public void setPaintList() {
		renderMap = new HashMap<String, Object>();
		renderMap.put("-1", defaultRender);
		
		if (cursor!=null && !cursor.isClosed()) {
			cursor.close();
		}
		cursor = dbHelper.getRenderItems(null);
		while (cursor.moveToNext()) {
			try {
			int renderId = cursor.getInt(cursor.getColumnIndex("ID"));
			int style = cursor.getInt(cursor.getColumnIndex("Style"));
			int strokeColor = cursor.getInt(cursor.getColumnIndex("StrokeColor"));
			int fillColor = cursor.getInt(cursor.getColumnIndex("FillColor"));
			float fontSize = cursor.getFloat(cursor.getColumnIndex("Fontsize"))*1.2f;/////////////////////////////
//			Log.d("wxq", "strokeColor="+strokeColor+",fillColor="+fillColor);
//			strokeColor = Color.YELLOW;
//			fillColor = Color.RED;
			String fontName = cursor.getString(cursor.getColumnIndex("FontName"));
			float strokeWidth = cursor.getFloat(cursor.getColumnIndex("StrokeWidth"))/2;
			Map<String, Object> item = new HashMap<String, Object>();
			item.put(DrawView.RENDER_STYLE_KEY, style+"");
			if (TextUtils.isEmpty(fontName)) {
				//非文字				
				if (style == DrawView.RENDER_STYLE_STROKE) {//stroke
					Paint strokePaint = new Paint();
					strokePaint.setAntiAlias(true);    //设置无锯齿
					strokePaint.setStyle(Style.STROKE);  //设置填充方式
					 int red = (strokeColor & 0xff0000) >> 16; 
					     int green = (strokeColor & 0x00ff00) >> 8; 
					     int blue = (strokeColor & 0x0000ff); 
					     strokePaint.setColor(Color.rgb( blue , green ,red)); 
					//strokePaint.setColor(strokeColor);
					    
					strokePaint.setStrokeWidth(strokeWidth);//线宽
					item.put(DrawView.PAINT_STROKE_KEY, strokePaint);
				}
				else if (style == DrawView.RENDER_STYLE_FILL) {//fill
					Paint fillPaint = new Paint();
					fillPaint.setAntiAlias(true);    //设置无锯齿
					fillPaint.setStyle(Style.FILL);  //设置填充方式
				      int red = (fillColor & 0xff0000) >> 16; 
					     int green = (fillColor & 0x00ff00) >> 8; 
					     int blue = (fillColor & 0x0000ff); 
					     fillPaint.setColor(Color.rgb( blue , green ,red)); 
					//fillPaint.setColor(fillColor);   //设置颜色
					item.put(DrawView.PAINT_FILL_KEY, fillPaint);
				}
				else if (style == DrawView.RENDER_STYLE_BOTH) {//both
					Paint fillPaint = new Paint();
					fillPaint.setAntiAlias(true);    //设置无锯齿
					fillPaint.setStyle(Style.FILL);  //设置填充方式
						 int red = (fillColor & 0xff0000) >> 16; 
					     int green = (fillColor & 0x00ff00) >> 8; 
					     int blue = (fillColor & 0x0000ff); 
					     fillPaint.setColor(Color.rgb( blue , green ,red)); 
					//fillPaint.setColor(fillColor);   //设置颜色
					item.put(DrawView.PAINT_FILL_KEY, fillPaint);
					
					Paint strokePaint = new Paint();
					strokePaint.setAntiAlias(true);    //设置无锯齿
					strokePaint.setStyle(Style.STROKE);  //设置填充方式
						 int red2 = (strokeColor & 0xff0000) >> 16; 
					     int green2 = (strokeColor & 0x00ff00) >> 8; 
					     int blue2 = (strokeColor & 0x0000ff); 
					     strokePaint.setColor(Color.rgb( blue2 , green2 ,red2));  
					//strokePaint.setColor(strokeColor);
					strokePaint.setStrokeWidth(strokeWidth);//线宽
					item.put(DrawView.PAINT_STROKE_KEY, strokePaint);
				}
			}
			else {
				//文字
				TextPaint fillPaint = new TextPaint();
				fillPaint.setAntiAlias(true);    //设置无锯齿
				fillPaint.setStyle(Style.FILL);  //设置填充方式
				fillPaint.setColor(fillColor);   //设置颜色
				fillPaint.setTextSize(fontSize);		 //设置字号
				if(fontName.equals("PalmapPublic"))
				{
					fillPaint.setTypeface(palmapPublicTF);
					fillPaint.setTextSize(40);
				}		
				item.put(DrawView.PAINT_FILL_KEY, fillPaint);
			}
			
			renderMap.put(renderId+"", item);
			} 
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		cursor.close();
	}
	
	
	@SuppressWarnings("unchecked")
	public void setShapeListWithId(String mapId) {
		shapeList = new ArrayList<Map<String,Object>>();
		if (cursor!=null && !cursor.isClosed()) {
			cursor.close();
		}
		cursor = dbHelper.getDrawItems(mapId);
		while (cursor.moveToNext()) {
			try {
//				Log.d("wxq", 
//						"renderID="+cursor.getString(cursor.getColumnIndex("RenderId"))
//						+"TextStr="+ cursor.getString(cursor.getColumnIndex("TextStr")));
				//int drawId = cursor.getInt(cursor.getColumnIndex("ID"));
				int type = cursor.getInt(cursor.getColumnIndex("Type"));
				int RenderId = cursor.getInt(cursor.getColumnIndex("RenderId"));
				int visible =  cursor.getInt(cursor.getColumnIndex("Visible"));
				String pathStr = cursor.getString(cursor.getColumnIndex("Path"));
				String spotId = cursor.getString(cursor.getColumnIndex("SpotId"));
				String textStr = cursor.getString(cursor.getColumnIndex("TextStr"));
				
				
				switch (type) {
				case 0://点
					MapShape pointShape = getPointShape(cursor);
					Map<String, Object> pointMap = new HashMap<String, Object>();
					pointMap.put(DrawView.SHAPE_KEY, pointShape);
					pointMap.put(DrawView.RENDER_KEY, renderMap.get(RenderId+""));
					pointMap.put(DrawView.VISIBLE_KEY, visible+"");
					pointMap.put(DrawView.PATH_KEY, pathStr);
					pointMap.put(DrawView.SPOT_ID_KEY, spotId);
					pointMap.put(DrawView.TEXT_KEY, textStr);
					shapeList.add(pointMap);
					break;
					
				case 1://线
					break;
				case 2://区块
					MapShape mapPath = getPathShape(cursor);
					Map<String, Object> mMap = new HashMap<String, Object>();
					mMap.put(DrawView.SHAPE_KEY, mapPath);
					mMap.put(DrawView.RENDER_KEY, renderMap.get(RenderId+""));
					mMap.put(DrawView.VISIBLE_KEY, visible+"");
					mMap.put(DrawView.PATH_KEY, pathStr);
					mMap.put(DrawView.SPOT_ID_KEY, spotId);
					mMap.put(DrawView.TEXT_KEY, textStr);
					shapeList.add(mMap);
					break;
					
				case 3://字
					MapShape textShape = getTextShape(cursor);
					Map<String, Object> textMap = new HashMap<String, Object>();
					textMap.put(DrawView.SHAPE_KEY, textShape);
					textMap.put(DrawView.RENDER_KEY, renderMap.get(RenderId+""));
					((MapText)textShape).setTextLayout((Paint) ( ((Map<String, Object>)( renderMap.get(RenderId+""))).get(DrawView.PAINT_FILL_KEY)));
					textMap.put(DrawView.VISIBLE_KEY, visible+"");
					textMap.put(DrawView.PATH_KEY, pathStr);
					textMap.put(DrawView.SPOT_ID_KEY, spotId);
					textMap.put(DrawView.TEXT_KEY, textStr);
					shapeList.add(textMap);
					break;
					
				default:
					break;
				}
				
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		cursor.close();
	}
	
	public MapShape getTextShape(Cursor c) {
		String pathStr = c.getString(c.getColumnIndex("Path"));
		String textStr = c.getString(c.getColumnIndex("TextStr"));
		String[] pointArray = pathStr.split(";");
		String[] topLeftPoint = pointArray[0].split(",");
		String[] rightBottomPoint = pointArray[1].split(",");
		float x = Float.parseFloat(topLeftPoint[0]) ;
		float y = Float.parseFloat(topLeftPoint[1]) ;
		float width = (Float.parseFloat(rightBottomPoint[0]) - x);
		
//		width*=2;/////////////////////////////////////////////////////////////////////////////////
//		x-=width/4;
//		y-=width/5;
		
		MapShape textShape = new MapText(x, y, textStr, width);
		
		return textShape;
		
	}
	public MapShape getPointShape(Cursor c) {
		String pathStr = c.getString(c.getColumnIndex("Path"));
		String[] pointArray = pathStr.split(",");
		float x = Float.parseFloat(pointArray[0]) ;
		float y = Float.parseFloat(pointArray[1]) ;
		MapShape pointShape = new MapPoint(x, y);
		return pointShape;
	}
	
	public MapShape getPathShape(Cursor c) {
		String pathStr = c.getString(c.getColumnIndex("Path"));
		String[] pointArray = pathStr.split(";");
		Path path = new Path();
		for (int i = 0; i < pointArray.length; i++) {
			String pointStr = pointArray[i];
			String [] point = pointStr.split(",");
			float x = Float.parseFloat(point[0]) ;
			float y = Float.parseFloat(point[1]) ;
			if (i==0) {
				path.moveTo(x, y);
			}
			else {
				path.lineTo(x, y);
			}
		}
		path.close();
		MapPath mapPath = new MapPath(path);
		
		return mapPath;
	}
	
	public void close() {
		
		if (cursor!=null && !cursor.isClosed()) {
			cursor.close();
		}
		
		if (dbHelper!=null) {
			dbHelper.close();
		}	
	}
	
	public String getSpotName(String spotId) {
		return dbHelper.getSpotInfo(spotId).get("Display");
	}

}
